//	ID: 066498650
//	Exercise #: 2
//	By:	Roy Moore
//
//	Written:	19/12/2011
//	Environment : VS2010
//
//-tcpServer.h-
//
//*Please include "ws2_32.lib" in the library list.
#ifndef __TCPSERVER_H
#define __TCPSERVER_H

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <winsock2.h>
#include <string.h>
#include <time.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//------------------Structures----------------------------------------------
struct SocketState
{
	SOCKET id;			// Socket handle
	int	recv;			// Recving State
	int	send;			// Sending State
	int sendSubType;	// Sending sub-type
	char buffer[4096];
	int len;
};
//------------------Constants-----------------------------------------------
const int HTTP_PORT 	= 80;
const int MAX_SOCKETS 	= 60;
const int EMPTY 		= 0;
const int LISTEN 		= 1;
const int RECEIVE		= 2;
const int IDLE 			= 3;
const int SEND			= 4;
const int SEND_TIME 	= 1;
const int SEND_SECONDS 	= 2;
const int TIMEOUT		= 120000;
const string CR_LF 		= "\r\n";
//------------------Enums---------------------------------------------------
enum HTTPREQ { // Defines HTTP Request methods
	NA		= -1 , 
	GET		= 1, 
	PUT		= 2, 
	HEAD	= 3, 
	DEL		= 4, 
	TRACE	= 5}; 
enum HTTPRSP { // Defines HTTP Response Codes
	OK				= 1, 
	NOT_FOUND		= 2, 
	SERVER_ERROR	= 3};
//------------------Prototypes----------------------------------------------
bool addSocket(SOCKET id, int what);
void removeSocket(int index);
void acceptConnection(int index);
void receiveMessage(int index);
HTTPREQ getMethod(int index);
void UpdateSocketToRequest(HTTPREQ method, int index, int bytesRecv, int len);
void sendMessage(int index);
void respondGet(int index);
void respondDel(int index);
void respondPut(int index);
void respondTrace(int index);
string getFullPath(int index);
void HeaderBuilder(string& msg,HTTPRSP code,int len);
void sendTcpMsg(SOCKET msgSocket,string msgStr);

#endif // __TCPSERVER_H